<template>
  <div class="personal-info">
    <el-form :model="userInfo" label-width="120px">
      <el-form-item>
        <el-avatar v-if="userInfo.avatar" :src="userInfo.avatar" shape="circle" :size="100">
        </el-avatar>
      </el-form-item>
      <el-form-item label="姓名：">
        <span>{{ userInfo.teacherName }}</span>
      </el-form-item>
      <el-form-item label="工号：">
        <span>{{ userInfo.employeeNumber }}</span>
      </el-form-item>
      <el-form-item label="邮箱：">
        <span>{{ userInfo.email }}</span>
      </el-form-item>
      <el-button type="danger" style="position: absolute;
      bottom: 80px; right: 240px;" @click="deleteHandle" icon="el-icon-warning-outline">注销账号</el-button>
      <!-- 修改个人信息的按钮 -->
      <el-button type="primary" style="position: absolute;
      bottom: 80px; right: 80px;" @click="modify">修改个人信息</el-button>
    </el-form>
  </div>
</template>

<script>

import { getUserIDLocalStorage } from "@/utils/utilLocalStorage";
import { getInfoAPI_Teacher } from "@/api/teacher/personal";
import {deleteTeacherAPI} from '@/api/teacher/personal'
export default {
  name: 'information',
  data() {
    return {
      userInfo: {
        avatar: '',
        teacherName: '',
        employeeNumber: '',
        email: '',
      },
    }
  },
  mounted() {
    this.getInfo()
  },
  methods: {
    onSuccess() {

    },
    beforeUpload() {

    },
    getInfo() {
      getInfoAPI_Teacher(getUserIDLocalStorage()).then((res) => {
        // console.log(res)
        this.userInfo = {
          avatar: res.data.avatar || '',
          teacherName: res.data.teacherName || '',
          employeeNumber: res.data.employeeNumber || '',
          email: res.data.email || '',
        };
      })
    },
    modify() {
      this.$router.push('/teacher/modify?id=' + getUserIDLocalStorage())
    },
    deleteHandle() {
      this.$confirm('确认注销, 是否继续?', '关于此教师所有信息被一并删除。确定注销？', {
        confirmButtonText: '注销',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteTeacher()
      })
    },
    async deleteTeacher() {
      if (getUserIDLocalStorage()) {
        try {
          const res = await deleteTeacherAPI(getUserIDLocalStorage())
          if (res.code === 200) {
            this.$message.success(res.message)
            // 跳回登录页
            this.$router.push({ path: '/login' });
          }
        } catch (error) {
          console.log(error)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.personal-info {
  //display: flex;
  //justify-content: center; /* 水平居中 */
  //align-items: center; /* 垂直居中 */
  margin-left: 240px;

  .span {
    width: 600px;
  }
}

.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
  height: 178px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
